import os, glob, pandas as pd
import matplotlib.pyplot as plt

os.makedirs("analysis", exist_ok=True)

files = sorted(glob.glob("logs/*.csv"))
if not files:
    print("No logs/*.csv files found. Run experiments first.")
    raise SystemExit(1)

dfs = []
for f in files:
    try:
        df = pd.read_csv(f)
        df['source_file'] = os.path.basename(f)
        dfs.append(df)
    except Exception as e:
        print("Skipping", f, "error", e)
if not dfs:
    print("No valid CSVs to process.")
    raise SystemExit(1)

all_df = pd.concat(dfs, ignore_index=True)

agg = all_df.groupby(['mode','strategy','outcome']).size().unstack(fill_value=0)
agg.to_csv("analysis/agg_summary.csv")
print("Saved aggregated summary: analysis/agg_summary.csv")
print(agg)
